<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ramais extends CI_Controller {

	function __construct() {
		parent::__construct();
		/*if(!$this->session->userdata('grambel')){
			$this->session->sess_destroy();
			redirect('login', 'refresh');
		}*/
	}
	
	public function index(){
		redirect('grambel/consulta');
	}
	
	public function novo(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		/*$setor = new Setor();
		$data["setores"] = $setor->consultaSetor();*/
		
		$secretaria = new Secretaria();
		$data["secretaria"] = $secretaria->consultaSecretaria();
		
		$data["action"] = 'ramais/salvar';
		$data["botao"] = 'Salvar';
		
		$this->load->view('header');
		$this->load->view('view_ramal',$data);
	}
	
	public function salvar(){	
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
	
		if($this->input->post()){
		
			$ramal = new Ramal();

			$ramal->setor_id = $this->input->post("setor");
			$ramal->ramal = $this->input->post("ramal");
			$ramal->observacao = strtoupper($this->input->post("observacao"));
			if($this->input->post("flag")){
				$ramal->flag = $this->input->post("flag");
			}else{
				$ramal->flag = '0';
			}

			if($ramal->save()){
			
				$acao = $ramal->check_last_query(false,true);
				$log = new Log();
				$log->ip = $this->session->userdata('ip_address');
				$log->usuario = $session_data["nome"];
				$log->acao = $acao;
				$log->save();
				
				echo 'RAMAL CADASTRADO';
			}else{
				echo $ramal->error->string;
			}

		
		}else{
			echo 'ERRO - EMPTY';
		
		
		}
	
	}
	public function editar($id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
	
		$ramal = new Ramal();
		
		//SALVA VIEW_RAMAL_EDITA
		if($this->input->post()){
		
			$ramal->where("id",$this->input->post("idramal"))->get();
			if($ramal->exists()){
				$ramal->setor_id = $this->input->post("setor");
				$ramal->ramal = $this->input->post("ramal");
				$ramal->observacao = strtoupper($this->input->post("observacao"));
				if($this->input->post("flag")){
					$ramal->flag = $this->input->post("flag");
				}else{
					$ramal->flag = '0';
				}

				if($ramal->save()){
					
					$acao = $ramal->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
					
					echo 'RAMAL ATUALIZADO';
				}else{
					echo $ramal->error->string;
				}
			}
		}else{
			
			//CARREGA VIEW_RAMAL_EDITA
			$ramal = $ramal->consultaEditarRamais($id);
			
			//echo $ramal->check_last_query();
			
			if($ramal->exists()){
				
				$data["ramal"] = $ramal;
				$data["action"] = 'ramais/editar';
				$data["botao"] = 'Editar';
			
			}else{
			
			
			}
			$secretaria = new Secretaria();
			$data["secretaria"] = $secretaria->consultaSecretaria();
			
			$this->load->view('header');
			$this->load->view('view_ramal_edita',$data);
			
		}
	
	}
	public function excluir($id = null){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if(!empty($id)){
			$ramal = new Ramal();
			$ramal->include_related("contato")->where("ramais.id",$id)->where("contato_id <>","null")->get();
			//echo $ramal->check_last_query();
			
			if($ramal->exists()){
				$contatos = array();
				foreach($ramal as $r){
				
					$contatos[] = $r->contato_id;
				
				}
				//print_r($contatos);
				echo "<script type='text/javascript' language='Javascript'>
				var resposta = confirm('Existem contatos cadastrados neste ramal. Deseja altera-los');";
				
				echo "if (resposta == true) {
					window.location.href = '".base_url('index.php/contatos/editarcontatos/'.$id)."';
				}else{
					alert('RAMAL NAO EXCLUIDO!')
					window.location.href = '".base_url('index.php/ramais/consulta/')."';
				}";
				
				echo "</script>";
				
			}else{
				$del = new Ramal();
				$del->where("id",$id)->get();
				
				if($del->delete()){
					$acao = $del->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
					
					echo "<script>alert('RAMAL EXCLUIDO!');</script>";
					echo "<script>window.location.href = '".base_url('index.php/ramais/consulta/')."';</script>";
				}else{
					echo "<script>alertify.alert('Erro ao excluir ramal: ". $del->error->string ."');</script>";
					echo "<script>window.location.href = '".base_url('index.php/ramais/consulta/')."'";
				}
			
			}
		}
	}
	public function consulta(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$this->load->view('header');
		$this->load->view('view_ramal_consulta');
	
	}
	public function consultaRamaisJson(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$ramal = new Ramal();
		echo $ramal->consultaRamais();
	
	}
	public function valida(){
	
		$ramal = new Ramal();
		$ramal->where("ramal",$this->input->get("ramal"))->get();
		if($ramal->exists()){ echo "false";}else{ echo "true";}
	
	}
	public function validaCadRamal(){
	
		$ramal = new Ramal();
		$ramal->where("ramal",$this->input->get("ramal"))->get();
		if($ramal->exists()){ echo "true";}else{ echo "false";}
	
	}
	public function selecionaRamal(){

		$ramal = new Ramal();
		$ramal = $ramal->where("setor_id",$this->input->get('id'))->order_by("ramal asc")->get();
		
		$data = array();
		foreach($ramal as $s){
			$data [] = array('optionValue'=>$s->id ,'optionDisplay' =>$s->ramal);
		}
	
		echo json_encode($data);
	
	
	}
	public function consultaJson(){

		$ramal = new Ramal();
		$ramal = $ramal->like("ramal",$this->input->get('term'))->order_by("ramal asc")->get();
		
		$data = array();
		foreach($ramal as $s){
			$data [] = array('value'=> $s->ramal ,'id' => $s->id);
		}
	
		echo json_encode($data);
	
	
	}
	public function relatorioramais(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			//------------------------------------------------------------------------------------------------------------------------
		
			$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			gerarRelatorio($pdf);
			$pdf->AddPage('P', 'A4');
			
			// TITULO DA PAGINA
			$pdf->Write(0, 'Lista de Ramais', '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			
			$sec = new Secretaria();
			$sec = $sec->relatorioramais();
			
			foreach($sec as $s){
				$ramal = new Ramal();
				$ramal = $ramal->relatorioramais($s->id);
			
				$pdf->SetFont('times', '', 9);
				$pdf->Ln();
				//TESTE TABELA
				$pdf->SetFont('times', 'B', 10);
				$pdf->Write(0, $s->sigla.' - '.$s->nomesecretaria, '', 0, 'C', true, 0, false, false, 0);
				$pdf->Ln();
				//*************************************************************************************
				// CABEÇALHO
				//*************************************************************************************
				$header = array('Telefone','Departamento','Funcionário');
				// Colors, line width and bold font
				$pdf->SetFillColor(208, 208, 208);
				$pdf->SetTextColor(0);
				$pdf->SetDrawColor(0, 0, 0);
				$pdf->SetLineWidth(0.3);
				$pdf->SetFont('times', 'B', 10);
				
					// Header
					$w = array(20,100,70);
					$num_headers = count($header);
					for($i = 0; $i < $num_headers; ++$i) {
						$pdf->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
					}
					$pdf->Ln();
					//*************************************************************************************
					// DADOS
					//*************************************************************************************
					// Color and font restoration
					$pdf->SetFillColor(224, 235, 255);
					$pdf->SetTextColor(0);
					$pdf->SetFont('times', '', 7);
					// Data
					$fill = 0;

				foreach($ramal as $r){	
				
					$pdf->Cell($w[0], 6,$r->ramal, 'LR', 0, 'L', $fill);
					$pdf->Cell($w[1], 6,$r->nomesetor, 'LR', 0, 'L', $fill);
					$pdf->Cell($w[2], 6,$r->nomecontato, 'LR', 0, 'L', $fill);
					$pdf->Ln();
					$fill=!$fill;
					
				}
				// FINALIZA TABELA
					$pdf->Cell(array_sum($w), 0, '', 'T');
				
			}		
			
			/*
			$html = "";
			$pdf->writeHTML($html, true, false, true, false, '');
			*/
			
			$pdf->Output('Relatorio_Devolucao.pdf', 'I');
			
			
		}else{
				
			$setor = new Setor();
			$data["setores"] = $setor->consultaSetor();
			
			/*$secretaria = new Secretaria();
			$data["secretaria"] = $secretaria->consultaSecretaria();*/
			
			$this->load->view('header');
			$this->load->view('view_ramal_relatorioramais',$data);
		}
	}
	public function relatorioocultos()	{
		if($this->input->post()){
			echo 'aqui';
		}else{
				
			$setor = new Setor();
			$data["setores"] = $setor->consultaSetor();
			
			/*$secretaria = new Secretaria();
			$data["secretaria"] = $secretaria->consultaSecretaria();*/
			
			$this->load->view('header');
			$this->load->view('view_ramal_relatorioocultos',$data);
		}
	}
	public function relatoriolocais(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
			//------------------------------------------------------------------------------------------------------------------------
		
			$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			gerarRelatorio($pdf);
			$pdf->AddPage('P', 'A4');
			
			// TITULO DA PAGINA
			$pdf->Write(0, 'Lista de Ramais', '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			
			$sec = new Secretaria();
			$sec = $sec->relatoriolocais($this->input->post("setores"));
			
			foreach($sec as $s){
				$ramal = new Ramal();
				$ramal = $ramal->relatoriolocais($s->setor_id);
			
				$pdf->SetFont('times', '', 9);
				$pdf->Ln();
				//TESTE TABELA
				$pdf->SetFont('times', 'B', 10);
				$pdf->Write(0, $s->sigla.' - '.$s->nomesecretaria, '', 0, 'C', true, 0, false, false, 0);
				$pdf->Ln();
				//*************************************************************************************
				// CABEÇALHO
				//*************************************************************************************
				$header = array('Telefone','Departamento','Funcionário');
				// Colors, line width and bold font
				$pdf->SetFillColor(208, 208, 208);
				$pdf->SetTextColor(0);
				$pdf->SetDrawColor(0, 0, 0);
				$pdf->SetLineWidth(0.3);
				$pdf->SetFont('times', 'B', 10);
				
				
				
					// Header
					$w = array(20,100,70);
					$num_headers = count($header);
					for($i = 0; $i < $num_headers; ++$i) {
						$pdf->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
					}
					$pdf->Ln();
					//*************************************************************************************
					// DADOS
					//*************************************************************************************
					// Color and font restoration
					$pdf->SetFillColor(224, 235, 255);
					$pdf->SetTextColor(0);
					$pdf->SetFont('times', '', 7);
					// Data
					$fill = 0;

				foreach($ramal as $r){	
				
					$pdf->Cell($w[0], 6,$r->ramal, 'LR', 0, 'L', $fill);
					$pdf->Cell($w[1], 6,$r->nomesetor, 'LR', 0, 'L', $fill);
					$pdf->Cell($w[2], 6,$r->nomecontato, 'LR', 0, 'L', $fill);
					$pdf->Ln();
					$fill=!$fill;
					
				}
				// FINALIZA TABELA
					$pdf->Cell(array_sum($w), 0, '', 'T');
				
			}		
			
			/*
			$html = "";
			$pdf->writeHTML($html, true, false, true, false, '');
			*/
			
			$pdf->Output('Relatorio_Devolucao.pdf', 'I');
			
		}else{
				
			$setor = new Setor();
			$data["setores"] = $setor->consultaSetor();
			
			/*$secretaria = new Secretaria();
			$data["secretaria"] = $secretaria->consultaSecretaria();*/
			
			$this->load->view('header');
			$this->load->view('view_ramal_relatoriolocais',$data);
		}
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */